package cn.yuanqiao.archive.guidang;

import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantYqdaDocument;
import cn.yuanqiao.archive.appManager.mapper.TenantCodeRuleMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantYqdaDocumentMapper;
import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.chengguojieshou.mapper.YqdaarticulatedMapper;
import cn.yuanqiao.archive.storeRoom.util.DateUtil;
import cn.yuanqiao.archive.wenjianzhengli.utils.FileUploadUtils;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.ZipUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.apache.commons.io.FileUtils;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

@Component
public class ArchiveOfOfficialDocument {
    private static Logger loger = LoggerFactory.getLogger(ArchiveOfOfficialDocument.class);
    @Autowired
    private ITenantSysConfigService sysConfigService;
    @Autowired
    private YqdaarticulatedMapper yqdaarticulatedMapper;
    @Autowired
    private TenantYqdaDocumentMapper documentMapper;
    @Autowired
    private TenantCodeRuleMapper tenantCodeRuleMapper;
    @Autowired
    private DataSource dataSource;

    private DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //租户编码 测试 A001 TODO
    //private String tenantCode = "A001";

    public static void main(String arg []){
        String path = "E:\\guidangpackage\\东检刑诉受[2022]770000000005号.zip";
        //File file = new File(path);
        //String name = file.getName();
        //System.out.println(name.endsWith("zip") && name.endsWith(").zip"));
        ArchiveOfOfficialDocument archiveOfOfficialDocument = new ArchiveOfOfficialDocument();
        String handle = archiveOfOfficialDocument.handle(path);
    }

    public String handle(String path) {

        StringBuilder builder = new StringBuilder("");

        long start=new Date().getTime();
        //处理租户编码
        //SecurityContextHolder.set("tenant_code", this.tenantCode);

        //获取临时解压路径前缀
        String yq_oatemp = sysConfigService.selectConfigByKey("YQ_OATEMP");
        if(!StringUtils.isNotEmpty(yq_oatemp) || "null".equals(yq_oatemp))
            yq_oatemp = "/data/";
        File file = new File(yq_oatemp);
        if(!file.exists()){
            file.mkdirs();
        }
        String zippedPath = yq_oatemp+"zipped"+System.currentTimeMillis()+"/";
        if(!new File(zippedPath).exists()) {
            new File(zippedPath).mkdirs();
        }
        loger.info("pathOfZip-------------{}",path);
        loger.info("zippedPath-------------{}",zippedPath);
        if(!zippedPath.endsWith("/")) zippedPath+="/";
        //获取解压编码
        String decompression_encoding = sysConfigService.selectConfigByKey("DECOMPRESSION_ENCODING");
        if(!StringUtils.isNotEmpty(decompression_encoding) || "null".equals(decompression_encoding))
            decompression_encoding = "UTF-8";
        ZipUtils.unZip(new File(path),zippedPath,decompression_encoding);  //解压

        File zippedPathfile = new File(zippedPath);
        int i1 = 0;
        if(zippedPathfile.isDirectory()) {
            File[] listFiles = zippedPathfile.listFiles();
            for(int i=0;i<listFiles.length;i++) {
                File file3 = listFiles[i];
                if(file3.isDirectory()) {
                    String concatPath = "";
                    concatPath+=file3.getName()+"/";
                    File[] listFiles2 = file3.listFiles();
                    for(int j=0;j<listFiles2.length;j++) {
                        File file2 = listFiles2[j];
                        String name = file2.getName();
                        if(name.endsWith("xml") && !name.endsWith(").xml") && !name.endsWith("）.xml")) {
                            concatPath+=name;
                            String filePath = zippedPath+concatPath;
                            //解析xml数据
                            Map<String, Object> analysisXml = null;  //案卷相关
                            Map<String, Object> analysisXmlOfDataHeadMap = null;  //包头信息
                            Map<String, Object> glnrMap = null;
                            try {
                                analysisXml = analysisXml(filePath);
                                analysisXmlOfDataHeadMap = analysisXmlOfDataHead(filePath);  //包头信息
                                glnrMap = analysisXmlOfGlxx(filePath);
                            } catch (Exception e) {
                                builder.append("XML文件解析失败；");
                                e.printStackTrace();
                                continue;
                            }
                            //数据入库
                            try {
                                i1 = insertData(analysisXml,zippedPath+file3.getName()+"/",analysisXmlOfDataHeadMap,glnrMap);
                                if(i1==0)
                                    builder.append("sql执行失败；");
                                if(i1==2)
                                    builder.append("案卷档号重复；");
                            } catch (IOException e) {
                                builder.append("相关原文操作异常；");
                                e.printStackTrace();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }

        long end=new Date().getTime();
        loger.info("归档包解析入库耗时：：：{}毫秒",(end-start));
        //File tempFile = new File(zippedPath);

        //boolean removeTempFile = removeTempFile(tempFile);
        //if(removeTempFile) loger.info("删除临时文件成功！");
        //else loger.info("删除临时文件失败！");
        return builder.toString();
    }

    public int insertData(Map<String, Object> map,String zippedPath,Map<String, Object> headMap,Map<String, Object> glnrMap) throws SQLException, IOException {
        String tenantCode = SecurityUtils.getTenantCode();
        boolean HD_ACL_ON_OFF = false;
        int executeUpdate = 0;
        ArrayList<String> filetempSaveColumn = new ArrayList<String>();
        ArrayList<String> foldertempSaveColumn = new ArrayList<String>();

        String qzh = "";  //   测试先写死001
        String filetablename = "HDDA_001_JC_25_FOLDER";
        String foldertablename = "HDDA_001_JC_25_FILE";
        String efiletablename = "";
        Long template_id = 31L;
        String type = "JC";
        Long folder_id = -1L;
        //测试先写死WS  TODO
        String lbh = "WS_JUAN_BZ";

        String yq_oacategory = sysConfigService.selectConfigByKey("YQ_OACATEGORY");
        //获取门类相关信息
        List<Map<String, Object>> maps = null;
        if(StringUtils.isNotEmpty(yq_oacategory) && !"null".equals(yq_oacategory)){
            String dm = String.valueOf(map.get("DAMLDM"));
            if(StringUtils.isEmpty(dm) || "null".equals(dm)) dm = "SS";
            String[] split = yq_oacategory.split(",");
            ArrayList<String> ls1 = new ArrayList<>();
            for(int i=0;i<split.length;i++){
                ls1.add(split[i]);
            }
            Map<String, String> collect = ls1.stream().collect(Collectors.toMap(s1 -> s1.split(":")[0], s2 -> s2.split(":")[1]));
            String folderTableName = collect.get(dm);
            maps = yqdaarticulatedMapper.executeSql("select id,file_table_name,fonds_code,group_id,CODE,folder_table_name,efile_table_name from "+tenantCode+"_category where folder_table_name = '"+folderTableName+"'");
        }else{
            //maps = yqdaarticulatedMapper.executeSql("select id,file_table_name,fonds_code,group_id,CODE,folder_table_name,efile_table_name from "+tenantCode+"_category where code = 'WS' and type = 1");
        }
        if(maps==null || maps.size()==0){
            throw new ServiceException("归档未找到对应门类！");
        }
        //List<Map<String, Object>> maps = yqdaarticulatedMapper.executeSql("select id,file_table_name,fonds_code,group_id,CODE,folder_table_name,efile_table_name from "+tenantCode+"_category where folder_table_name = '"++"'");
        Map<String, Object> map1 = maps.get(0);
        filetablename = (String) map1.get("FILE_TABLE_NAME");
        qzh = (String) map1.get("FONDS_CODE");
        template_id = (Long)map1.get("GROUP_ID");
        type = (String) map1.get("CODE");
        foldertablename = (String) map1.get("FOLDER_TABLE_NAME");
        efiletablename = (String) map1.get("EFILE_TABLE_NAME");
        folder_id = SnowflakeIdGenerator.generateId();
        String categoryId = String.valueOf(map1.get("ID"));

        TenantCategory tenantCategory = new TenantCategory();
        tenantCategory.setCode(type);
        tenantCategory.setFondsCode(qzh);
        tenantCategory.setFileTableName(filetablename);

        //判断案卷档号是否重复
       /* String vdh = String.valueOf(map.get("DH"));
        if(StringUtils.isNotEmpty(vdh) && !"null".equals(vdh)){
            TenantCodeRule codeRule = new TenantCodeRule();
            codeRule.setCategoryModelId(Long.valueOf(categoryId));
            List<TenantCodeRule> tenantCodeRules = tenantCodeRuleMapper.selectYqdaCoderuleModelList(codeRule);
            for(TenantCodeRule t:tenantCodeRules){
                String vdhcolumn = t.getvColumnname();
                if(StringUtils.isNotEmpty(vdhcolumn) && !"null".equals(vdhcolumn)){
                    List<Map<String, Object>> maps1 = yqdaarticulatedMapper.executeSql("select id from " + foldertablename + " where " + vdhcolumn + " = '" + vdh + "' and is_del = 0");
                    if(maps1!=null && maps1.size()!=0){
                        //档号已存在
                        executeUpdate = 2;
                        return executeUpdate;
                    }
                }
            }
        }*/


        //数据包头sql
        String insertHeadSql = insertHeadSql(headMap,folder_id);
        //当事人信息sql
        String insertDsrxxSql = insertDsrxxSql(map,folder_id);
        //插入案件信息sql
        String insertAjxxSql = insertAjxxSql(map,folder_id);
        //插入犯罪嫌疑人sql
        String insertFzxyrSql = insertFzxyrSql(map,folder_id);
        //插入被害人sql
        String insertBhrSql = insertBhrSql(map,folder_id);
        //插入管理内容sql
        String insertGFlnrSql = insertGFlnrSql(glnrMap, folder_id);
        //插入过程信息sql
        String insertGcxxSql = insertGcxxSql(glnrMap, folder_id);
        //插入备考表信息
        String insertBkbSql = insertBkbSql(map, folder_id);

        Map<String, Map<String, String>> dictByGroupId = getDictByGroupId(template_id);  //取文书组件的 group_id
        loger.info("元数据方案：{}，所有字典项：：：{}",template_id,dictByGroupId.toString());
        //处理文件类型字典
        /*List<TenantSysDictData> sysDictDatas = tenantSysDictTypeService.selectDictDataByType("TYPE");
        List<Map<String,Object>> loadDATypeCode = new ArrayList<>();
        for(int i=0;i<sysDictDatas.size();i++){
            TenantSysDictData tenantSysDictData = sysDictDatas.get(i);
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name",tenantSysDictData.getDictLabel());
            map1.put("code",tenantSysDictData.getDictValue());
            loadDATypeCode.add(map1);
        }*/
        //拿到对应关系 字段类型和数据库字段名称
        Map<String, Object> filemdataList = getMdataList(template_id,0);
        loger.info("filemdataList---relation：：：{}",filemdataList.toString());
        Map<String, Object> foldermdataList = getMdataList(template_id,1);
        loger.info("foldermdataList---relation：：：{}",foldermdataList.toString());
        //处理保管期限
        //getBgqxByRelation(map,template_id);

        Set<Entry<String,Object>> entrySet = map.entrySet();
        Iterator<Entry<String, Object>> iterator = entrySet.iterator();

        StringBuffer hddafolderSql = new StringBuffer();
        StringBuilder vcolumnsBuilder = new StringBuilder();
        StringBuilder vvalueBuilder = new StringBuilder();
        HashMap<String,String> fileSqlList = new HashMap<String, String>();
        HashMap<String,String> fileKeySqlList = new HashMap<String, String>();
        ArrayList<String> docSqlList = new ArrayList<String>();
        //List<String> fileSqlList = new ArrayList<String>();
        StringBuilder fcolumnsBuilder = new StringBuilder();
        StringBuilder fvalueBuilder = new StringBuilder();
        String docSql = "";
        while(iterator.hasNext()) {
            Entry<String, Object> next = iterator.next();
            String key = next.getKey();
            Object value = next.getValue();
            //拼接hdda数据
            if(!"children".equals(key)) {
                if(foldermdataList.containsKey(key)) {
                    String concatVal = String.valueOf(foldermdataList.get(key));
                    String[] valArray = concatVal.split(":");
                    String field_name = valArray[0];  //数据库字段名
                    String fieldtype = valArray[1];   //字段
                    String showtype = valArray[2];	  //展示类型
                    vcolumnsBuilder.append(field_name).append(",");
                    foldertempSaveColumn.add(field_name);   //记录
                    if("5".equals(showtype.trim())) {  //如果有字典项
                        Map<String, String> dictMap = dictByGroupId.get(field_name);
                        String dictVal = dictMap.get(value);
                        if(!StringUtils.isEmpty(dictVal)) {
                            value = dictVal;
                        }
                    }
                    //System.out.println(field_name+":"+fieldtype.trim());
                    if("1".equals(fieldtype.trim())) {   //整型
                        vvalueBuilder.append(value).append(",");
                    }else if("2".equals(fieldtype.trim())) {   //字符型
                        vvalueBuilder.append("'"+value+"',");
                    }else{
                        vvalueBuilder.append("'"+value+"',");
                    }
                }
                //拼接hddadocument数据
            }else {
                ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>)map.get("children");
                for(int i=0;i<list.size();i++) {
                    Long da_id = SnowflakeIdGenerator.generateId();
                    hddafolderSql.append("insert into "+tenantCode+"_yqda_folder(da_id,folder_id) values("+da_id+","+folder_id+");");
                    Map<String, Object> map2 = list.get(i);
                    Set<Entry<String,Object>> entrySet2 = map2.entrySet();
                    Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                    while(iterator2.hasNext()) {
                        Entry<String, Object> next2 = iterator2.next();
                        String key2 = next2.getKey();
                        Object value2 = next2.getValue();
                        if(!"children".equals(key2)) {
                            if(filemdataList.containsKey(key2)) {
                                String concatVal = String.valueOf(filemdataList.get(key2));
                                String[] valArray = concatVal.split(":");
                                String field_name = valArray[0];  //数据库字段名
                                String fieldtype = valArray[1];   //字段
                                String showtype = valArray[2];	  //展示类型
                                if(i==0) {   //卷内件列名只保存一遍
                                    filetempSaveColumn.add(field_name);   //记录
                                }
                                fcolumnsBuilder.append(field_name).append(",");
                                if("5".equals(showtype.trim())) {  //如果有字典项
                                    Map<String, String> dictMap = dictByGroupId.get(field_name);
                                    String dictVal = dictMap.get(value2);
                                    if(!StringUtils.isEmpty(dictVal)) {
                                        value2 = dictVal;
                                    }
                                }
                                if("1".equals(fieldtype.trim())) {   //整型
                                    fvalueBuilder.append(value2).append(",");
                                }else if("2".equals(fieldtype.trim())) {   //字符型
                                    fvalueBuilder.append("'"+value2+"',");
                                }else{
                                    fvalueBuilder.append("'"+value2+"',");
                                }
                            }
                            //拼接hddadocument数据
                        }else {
                            ArrayList<Map<String, Object>> list2 = (ArrayList<Map<String, Object>>)map2.get("children");

                            docSql = getDocSql(list2,da_id,dictByGroupId,zippedPath,efiletablename,tenantCategory,template_id);

                            docSqlList.add(docSql);

                        }

                    }
                    fileSqlList.put(String.valueOf(da_id), fvalueBuilder.toString());
                    fileKeySqlList.put(String.valueOf(da_id),fcolumnsBuilder.toString());
                    //fileSqlList.add(fvalueBuilder.toString());
                    fvalueBuilder.setLength(0);
                    fcolumnsBuilder.setLength(0);
                }

            }
        }

        //拼接跟卷相关联的pdf
        String pdfSql = getPdfSql(folder_id, zippedPath, efiletablename, tenantCategory);


        //根据归档处室代码查询档案系统中归档处室id
        //String gdcs_id = getGdcs_id(map.get("gdcsdm"));
        //String jgCode = getJg_code(map.get("gdcsdm"));


        //fvalueBuilder.append("0,1,0,'").append(DateUtil.getDateString(new Date())).append("','").append(qzh).append("','").append(type).append("',");
        //.append(gdcs_id).append(",")
        //.append("'").append(jgCode).append("',");
        List<Map<String,Object>> foldernotEmptyColumn = getNotEmptyColumn(template_id,1);
        loger.info("foldernotEmptyColumn：：：{}",foldernotEmptyColumn.toString());
        for(Map<String,Object> map2:foldernotEmptyColumn) {
            String field_name = map2.get("FIELD_NAME").toString();
            if(!foldertempSaveColumn.contains(field_name)) {
                String fieldtype = map2.get("FIELDTYPE").toString();
                if("1".equals(fieldtype)) {  //整型
                    vcolumnsBuilder.append(field_name).append(",");
                    vvalueBuilder.append("0,");
                }else if("2".equals(fieldtype)) {  //字符型
                    vcolumnsBuilder.append(field_name).append(",");
                    vvalueBuilder.append("'',");
                }
            }
        }
        vcolumnsBuilder.append("IS_DEL,STATE,CREATE_TIME,QZH,TYPE,");
        String vcolumnsString = vcolumnsBuilder.toString();
        String vvalueString = vvalueBuilder.toString();
        vvalueString += "0,1,'"+DateUtil.getDateString(new Date())+"','"+qzh+"','"+type+"',";
        String vinsertDaSql = "insert into "+tenantCode+"_yqfolder(JS_TYPE,"+(HD_ACL_ON_OFF?"acl,":"")+"folder_id,"+vcolumnsString+"sub_table_name) values(1,"+(HD_ACL_ON_OFF?"11,":"")+""+folder_id+","+vvalueString+"'"+foldertablename+"');";
        vinsertDaSql += "insert into "+foldertablename+"(JS_TYPE,"+(HD_ACL_ON_OFF?"acl,":"")+"id,"+vcolumnsString.substring(0,vcolumnsString.length()-1)+") values(1,"+(HD_ACL_ON_OFF?"11,":"")+""+folder_id+","+vvalueString.substring(0,vvalueString.length()-1)+");";


        //fvalueBuilder.append("0,1,0,'").append(DateUtil.getDateString(new Date())).append("','").append(qzh).append("','").append(type).append("',");
        //.append(gdcs_id).append(",")
        //.append("'").append(jgCode).append("',");
        List<Map<String,Object>> filenotEmptyColumn = getNotEmptyColumn(template_id,0);
        loger.info("filenotEmptyColumn：：：{}",filenotEmptyColumn.toString());
        for(Map<String,Object> map2:filenotEmptyColumn) {
            String field_name = map2.get("FIELD_NAME").toString();
            if(!filetempSaveColumn.contains(field_name)) {
                String fieldtype = map2.get("FIELDTYPE").toString();
                if("1".equals(fieldtype)) {  //整型
                    //fcolumnsBuilder.append(field_name).append(",");
                    Set<Entry<String, String>> entries = fileKeySqlList.entrySet();
                    Iterator<Entry<String, String>> iterator1 = entries.iterator();
                    while (iterator1.hasNext()){
                        Entry<String, String> next = iterator1.next();
                        String value3 = next.getValue();
                        String key = next.getKey();
                        fileKeySqlList.put(key,value3+field_name+",");
                    }


                    Set<Entry<String,String>> entrySet2 = fileSqlList.entrySet();
                    //fvalueBuilder.append("0,");
                    Iterator<Entry<String, String>> iterator2 = entrySet2.iterator();
                    while(iterator2.hasNext()) {
                        Entry<String, String> next = iterator2.next();
                        String value3 = next.getValue();
                        String key = next.getKey();
                        fileSqlList.put(key, value3+"0,");
                        //value3 = value3+"0,";

                    }
                }else if("2".equals(fieldtype)) {  //字符型
                    //fcolumnsBuilder.append(field_name).append(",");
                    Set<Entry<String, String>> entries = fileKeySqlList.entrySet();
                    Iterator<Entry<String, String>> iterator1 = entries.iterator();
                    while (iterator1.hasNext()){
                        Entry<String, String> next = iterator1.next();
                        String value3 = next.getValue();
                        String key = next.getKey();
                        fileKeySqlList.put(key,value3+field_name+",");
                    }
                    Set<Entry<String,String>> entrySet2 = fileSqlList.entrySet();
                    //fvalueBuilder.append("0,");
                    Iterator<Entry<String, String>> iterator2 = entrySet2.iterator();
                    while(iterator2.hasNext()) {
                        Entry<String, String> next = iterator2.next();
                        String value3 = next.getValue();
                        String key = next.getKey();
                        fileSqlList.put(key, value3+"'',");
                        //value3 = value3+"'null',";
                    }
                    //fvalueBuilder.append("'null',");
                }
            }
        }
        Set<Entry<String, String>> entries = fileKeySqlList.entrySet();
        Iterator<Entry<String, String>> iterator1 = entries.iterator();
        String commonValue = "IS_DEL,STATE,IS_INDEX,CREATE_TIME,QZH,TYPE,";
        while (iterator1.hasNext()){
            Entry<String, String> next = iterator1.next();
            String key = next.getKey();
            String value = next.getValue();
            fileKeySqlList.put(key,value+commonValue);
        }
        //fcolumnsBuilder.append("IS_DEL,STATE,IS_INDEX,CREATE_TIME,QZH,TYPE,");
        //String columnsString = fcolumnsBuilder.toString();
        //String valueString = valueBuilder.toString();

        String insertDaSql = "";
        Set<Entry<String,String>> entrySet2 = fileSqlList.entrySet();
        Iterator<Entry<String, String>> iterator2 = entrySet2.iterator();
        while(iterator2.hasNext()) {
            Entry<String, String> next = iterator2.next();
            String key = next.getKey();
            String col = fileKeySqlList.get(key);
            String value = next.getValue();
            //fvalueBuilder.append("0,1,0,'").append(DateUtil.getDateString(new Date())).append("','").append(qzh).append("','").append(type).append("',");
            value=value+"0,1,0,'"+ DateUtil.getDateString(new Date())+"','"+qzh+"','"+type+"',";
            insertDaSql += "insert into "+tenantCode+"_yqda(JS_TYPE,"+(HD_ACL_ON_OFF?"acl,":"")+"da_id,"+col+"sub_table_name) values(1,"+(HD_ACL_ON_OFF?"11,":"")+""+key+","+value+"'"+filetablename+"');";
            insertDaSql += "insert into "+filetablename+"(JS_TYPE,"+(HD_ACL_ON_OFF?"acl,":"")+"id,"+col.substring(0,col.length()-1)+") values(1,"+(HD_ACL_ON_OFF?"11,":"")+""+key+","+value.substring(0,value.length()-1)+");";
        }


        //String insertDaSql = "insert into hdda(da_id,"+columnsString+"sub_table_name) values("+da_id+","+valueString+"'"+tableName+"');";
        //insertDaSql += "insert into "+filetablename+"(id,"+columnsString.substring(0,columnsString.length()-1)+") values("+da_id+","+valueString.substring(0,valueString.length()-1)+");";
        String finalSqlString = insertDaSql+vinsertDaSql+hddafolderSql.toString();
        for(int i=0;i<docSqlList.size();i++) {
            String sql = docSqlList.get(i);
            loger.info("OA接口消息插入docsql"+i+"：：：{}",sql);
            finalSqlString+=sql;
        }
        finalSqlString+=insertDsrxxSql+insertBhrSql+insertAjxxSql+insertFzxyrSql+insertHeadSql+insertGFlnrSql+insertBkbSql+pdfSql+insertGcxxSql;
        loger.info("OA接口消息插入sql1：：：{}",insertDaSql);
        loger.info("OA接口消息插入sql2：：：{}",vinsertDaSql);
        loger.info("OA接口消息插入sql3：：：{}",hddafolderSql.toString());
        loger.info("当事人信息sql4：：：{}",insertDsrxxSql);
        loger.info("被害人信息sql5：：：{}",insertBhrSql);
        loger.info("案件信息sql6：：：{}",insertAjxxSql);
        loger.info("犯罪嫌疑人信息sql7：：：{}",insertFzxyrSql);
        loger.info("包头信息sql8：：：{}",insertHeadSql);
        loger.info("管理内容信息sql9：：：{}",insertGFlnrSql);
        loger.info("备考表信息sql10：：：{}",insertBkbSql);
        loger.info("pdf信息sql10：：：{}",pdfSql);
        loger.info("过程信息sql11：：：{}",insertGcxxSql);
        String[] split = finalSqlString.split(";");
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = dataSource.getConnection();
            conn.setAutoCommit(false);//启动事务
            stmt = conn.createStatement();
            for(String sql:split){
                if(StringUtils.isNotEmpty(sql)){
                    stmt.execute(sql);
                }
            }
            conn.commit();//提交事务
            executeUpdate = 1;   //sql执行成功
        }catch (SQLException e){
            conn.rollback();
            e.printStackTrace();
        }finally {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
        return executeUpdate;
    }

    public String getPdfSql(Long folder_id, String zippedPath, String efiletablename, TenantCategory tenantCategory) throws IOException {
        String pdfSql = "";
        File file = new File(zippedPath);
        String yq_mnt = sysConfigService.selectConfigByKey("YQ_MNT");
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for(int i=0;i<files.length;i++){
                File file1 = files[i];
                String name = file1.getName();
                if(name.endsWith("pdf")){
                    String suffix = name.substring(name.lastIndexOf(".")+1);
                    int type1 = name.startsWith("备考表")?1:name.startsWith("电子文件")?2:name.startsWith("卷内目录")?3:name.startsWith("卷皮")?4:0;
                    String savefilePath = FileUploadUtils.extractPath(tenantCategory,folder_id);  //文件路径
                    String savefileName = extractFilename(type1,folder_id,type1,suffix);  //保存文件名
                    Long doc_id = SnowflakeIdGenerator.generateId();
                    pdfSql+="insert into "+efiletablename+"(doc_id,doc_name,con_table_id,type,doc_order,is_del,create_time,is_index,real_doc_name,doc_mnt,path) values(" +
                            ""+doc_id+",'"+name+"',"+folder_id+","+type1+","+type1+",0,'"+DateUtils.getTime()+"',0,'"+savefileName+"','"+yq_mnt+"','"+savefilePath+savefileName+"');";
                    File newFile = new File(yq_mnt+savefilePath+savefileName);
                    loger.info("{}文件路径：：：{}",name,yq_mnt+savefilePath+savefileName);
                    FileUtils.copyFile(file1,newFile);
                }
            }
        }
        return pdfSql;
    }

    public String insertHeadSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        String finalSql = "";
        Map<String, Object> map2 = map;
        if(null!=map2){
            Long id = SnowflakeIdGenerator.generateId();
            String sql="insert into "+tenantCode+"_DATAHEAD(id,folder_id,";
            String sql2="values("+id+","+folder_id+",";
            Set<Entry<String,Object>> entrySet = map2.entrySet();
            Iterator<Entry<String, Object>> iterator = entrySet.iterator();
            while(iterator.hasNext()) {
                Entry<String, Object> next = iterator.next();
                String key = next.getKey();
                String value = String.valueOf(next.getValue());
                sql+=""+key+",";
                sql2+="'"+value+"',";
            }
            if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
            if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
            String concatSql = sql+") "+sql2+");";
            finalSql+=concatSql;
        }
        return finalSql;
    }
    public String insertBkbSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        Map<String, Object> o = (Map<String, Object>) map.get("BKB");
        String finalSql = "";
        Map<String, Object> map2 = o;
        if(null!=map2){
            Long id = SnowflakeIdGenerator.generateId();
            String sql="insert into "+tenantCode+"_BKB(id,folder_id,";
            String sql2="values("+id+","+folder_id+",";
            Set<Entry<String,Object>> entrySet = map2.entrySet();
            Iterator<Entry<String, Object>> iterator = entrySet.iterator();
            while(iterator.hasNext()) {
                Entry<String, Object> next = iterator.next();
                String key = next.getKey();
                String value = String.valueOf(next.getValue());
                sql+=""+key+",";
                sql2+="'"+value+"',";
            }
            if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
            if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
            String concatSql = sql+") "+sql2+");";
            finalSql+=concatSql;
        }
        return finalSql;
    }

    public String insertGFlnrSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        String finalSql = "";
        if(null!=map){
            Set<Entry<String, Object>> entries = map.entrySet();
            Iterator<Entry<String, Object>> iterator = entries.iterator();
            while (iterator.hasNext()){
                Entry<String, Object> next = iterator.next();
                String key = next.getKey();
                Object value = next.getValue();
                if(value instanceof Map){
                    Map<String,Object> map2 = (Map<String,Object>)value;
                    Long id = SnowflakeIdGenerator.generateId();
                    String sql="insert into "+tenantCode+"_"+key+"(id,folder_id,";
                    String sql2="values("+id+","+folder_id+",";
                    Set<Entry<String,Object>> entrySet2 = map2.entrySet();
                    Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                    int count = 0;
                    while(iterator2.hasNext()) {
                        Entry<String, Object> next2 = iterator2.next();
                        String key2 = next2.getKey();
                        String value2 = String.valueOf(next2.getValue());
                        sql+=""+key2+",";
                        sql2+="'"+value2+"',";
                        if(StringUtils.isEmpty(value2) || "null".equals(value2)) count++;
                    }
                    if(count==map2.size()) continue;
                    if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                    if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                    String concatSql = sql+") "+sql2+");";
                    finalSql+=concatSql;
                }else
                if(value instanceof List){
                    List<Map<String,Object>> list2 = (List<Map<String,Object>>)value;
                    for(int i=0;i<list2.size();i++){
                        Map<String, Object> map2 = list2.get(i);
                        Long id = SnowflakeIdGenerator.generateId();
                        String sql="insert into "+tenantCode+"_"+key+"(id,folder_id,";
                        String sql2="values("+id+","+folder_id+",";
                        Set<Entry<String,Object>> entrySet2 = map2.entrySet();
                        Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                        int count = 0;
                        while(iterator2.hasNext()) {
                            Entry<String, Object> next2 = iterator2.next();
                            String key2 = next2.getKey();
                            String value2 = String.valueOf(next2.getValue());
                            sql+=""+key2+",";
                            sql2+="'"+value2+"',";
                            if(StringUtils.isEmpty(value2) || "null".equals(value2)) count++;
                        }
                        if(count==map2.size()) continue;
                        if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                        if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                        String concatSql = sql+") "+sql2+");";
                        finalSql+=concatSql;
                    }
                }
            }
        }
        return finalSql;
    }
    public String insertGcxxSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        String finalSql = "";
        if(null!=map){
            Set<Entry<String, Object>> entries = map.entrySet();
            Iterator<Entry<String, Object>> iterator = entries.iterator();
            while (iterator.hasNext()){
                Entry<String, Object> next = iterator.next();
                String key = next.getKey();
                if("GLYWGLXX".equalsIgnoreCase(key)){

                    Object value = next.getValue();
                    if(value instanceof Map){
                        Map<String,Object> map2 = (Map<String,Object>)value;
                        Long id = SnowflakeIdGenerator.generateId();
                        String sql="insert into "+tenantCode+"_"+key+"(id,folder_id,";
                        String sql2="values("+id+","+folder_id+",";
                        Set<Entry<String,Object>> entrySet2 = map2.entrySet();
                        Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                        int count = 0;
                        while(iterator2.hasNext()) {
                            Entry<String, Object> next2 = iterator2.next();
                            String key2 = next2.getKey();
                            String value2 = String.valueOf(next2.getValue());
                            sql+=""+key2+",";
                            sql2+="'"+value2+"',";
                            if(StringUtils.isEmpty(value2) || "null".equals(value2)) count++;
                        }
                        if(count==map2.size()) continue;
                        if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                        if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                        String concatSql = sql+") "+sql2+");";
                        finalSql+=concatSql;
                    }else
                    if(value instanceof List){
                        List<Map<String,Object>> list2 = (List<Map<String,Object>>)value;
                        for(int i=0;i<list2.size();i++){
                            Map<String, Object> map2 = list2.get(i);
                            String idH = String.valueOf(map2.get("IDH"));
                            if(!StringUtils.isEmpty(idH) && !"null".equals(idH)){
                                String glsj = isNullThenDefault(map2.get("GLSJ"));
                                if(!glsj.contains(":")) {
                                    glsj += " 00:00:00";
                                }
                                Long id = SnowflakeIdGenerator.generateId();
                                String sql="insert into "+tenantCode+"_BUSINESSACTIVITY(id,name,user_name,action_time,dept,da_id";
                                String sql2="values("+id+",'"+isNullThenDefault(map2.get("GLXW"))+"','"+isNullThenDefault(map2.get("GLR"))+"','"+isNullThenDefault(glsj)+"','"+isNullThenDefault(map2.get("BM"))+"',"+folder_id+"";
                                /*Set<Entry<String,Object>> entrySet2 = map2.entrySet();
                                Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                                int count = 0;
                                while(iterator2.hasNext()) {
                                    Entry<String, Object> next2 = iterator2.next();
                                    String key2 = next2.getKey();
                                    String value2 = String.valueOf(next2.getValue());
                                    sql+=""+key2+",";
                                    sql2+="'"+value2+"',";
                                    if(StringUtils.isEmpty(value2) || "null".equals(value2)) count++;
                                }
                                if(count==map2.size()) continue;
                                if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                                if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);*/
                                String concatSql = sql+") "+sql2+");";
                                finalSql+=concatSql;
                            }
                        }
                    }
                }
            }
        }
        return finalSql;
    }
    public String isNullThenDefault(Object object) {
        String obj = String.valueOf(object);
        if(StringUtils.isEmpty(obj) || "null".equals(obj)) {
            return "";
        }
        return obj;
    }
    public String insertDsrxxSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        ArrayList<Map<String, Object>> list =(ArrayList<Map<String, Object>>)map.get("DSRXXLIST");
        String finalSql = "";
        if(null!=list){
            if(list.size()!=0) {
                for(int i=0;i<list.size();i++) {
                    Map<String, Object> map2 = list.get(i);
                    Long id = SnowflakeIdGenerator.generateId();
                    String sql="insert into "+tenantCode+"_DSRXX(id,folder_id,";
                    String sql2="values("+id+","+folder_id+",";
                    Set<Entry<String,Object>> entrySet = map2.entrySet();
                    Iterator<Entry<String, Object>> iterator = entrySet.iterator();
                    int count = 0;
                    while(iterator.hasNext()) {
                        Entry<String, Object> next = iterator.next();
                        String key = next.getKey();
                        String value = String.valueOf(next.getValue());
                        sql+=""+key+",";
                        sql2+="'"+value+"',";
                        if(StringUtils.isEmpty(value) || "null".equals(value)) count++;
                    }
                    //都为空的话 跳过 不插入数据库
                    if(count==map2.size()) continue;
                    if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                    if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                    String concatSql = sql+") "+sql2+");";
                    finalSql+=concatSql;
                }
            }
        }
        return finalSql;
    }
    public String insertBhrSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        ArrayList<Map<String, Object>> list =(ArrayList<Map<String, Object>>)map.get("BHRLIST");
        String finalSql = "";
        if(null!=list){
            if(list.size()!=0) {
                for(int i=0;i<list.size();i++) {
                    Map<String, Object> map2 = list.get(i);
                    Set<Entry<String,Object>> entrySet = map2.entrySet();
                    Iterator<Entry<String, Object>> iterator = entrySet.iterator();
                    while(iterator.hasNext()) {
                        Entry<String, Object> next = iterator.next();
                        Long id = SnowflakeIdGenerator.generateId();
                        String key = next.getKey();
                        String sql1 = "insert into "+tenantCode+"_akbhr(id,name,folder_id) values("+id+",'"+key+"',"+folder_id+");";
                        finalSql+=sql1;
                        Long id2 = SnowflakeIdGenerator.generateId();;
                        String sql="insert into "+tenantCode+"_AKBHRXX(id,folder_id,bhr_id,";
                        String sql2="values("+id2+","+folder_id+","+id+",";
                        Map<String, Object> value = (HashMap<String, Object>)next.getValue();
                        Set<Entry<String,Object>> entrySet2 = value.entrySet();
                        Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                        int count = 0;
                        while(iterator2.hasNext()) {
                            Entry<String, Object> next2 = iterator2.next();
                            String key2 = next2.getKey();
                            String value2 = String.valueOf(next2.getValue());
                            sql+=""+key2+",";
                            sql2+="'"+value2+"',";
                            if(StringUtils.isEmpty(value2) || "null".equals(value2)) count++;
                        }
                        if(count==value.size()) continue;
                        if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                        if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                        String concatSql = sql+") "+sql2+");";
                        finalSql+=concatSql;
                    }

                }
            }
        }
        return finalSql;
    }

    public String insertFzxyrSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        ArrayList<Map<String, Object>> list =(ArrayList<Map<String, Object>>)map.get("FZXYRLIST");
        String finalSql = "";
        if(null==list){
            return finalSql;
        }
        loger.info("Fzxyr：：：xml：：：{}",list.toString());

        if(null!=list){
            if(list.size()!=0) {
                for(int i=0;i<list.size();i++) {
                    Map<String, Object> map2 = list.get(i);
                    Set<Entry<String,Object>> entrySet = map2.entrySet();
                    Iterator<Entry<String, Object>> iterator = entrySet.iterator();
                    while(iterator.hasNext()) {
                        Entry<String, Object> next = iterator.next();
                        Long id = SnowflakeIdGenerator.generateId();
                        String key = next.getKey();
                        String sql1 = "insert into "+tenantCode+"_AKFZXYR(id,name,folder_id) values("+id+",'"+key+"',"+folder_id+");";
                        finalSql+=sql1;
                        Map<String, Object> value = (HashMap<String, Object>)next.getValue();
                        Set<Entry<String,Object>> entrySet2 = value.entrySet();
                        Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                        while(iterator2.hasNext()) {
                            Entry<String, Object> next2 = iterator2.next();
                            String key2 = next2.getKey();   //YSCPQK
                            Long id2 = SnowflakeIdGenerator.generateId();
                            String sql="insert into "+tenantCode+"_AKFZ"+key2+"(id,folder_id,bhr_id,";
                            String sql2="values("+id2+","+folder_id+","+id+",";

                            Map<String, Object> value2 = (HashMap<String, Object>)next2.getValue();
                            Set<Entry<String,Object>> entrySet3 = value2.entrySet();
                            Iterator<Entry<String, Object>> iterator3 = entrySet3.iterator();
                            int count = 0;
                            while(iterator3.hasNext()) {
                                Entry<String, Object> next3 = iterator3.next();
                                String key3 = next3.getKey();
                                String value3 = String.valueOf(next3.getValue());
                                sql+=""+key3+",";
                                sql2+="'"+value3+"',";
                                if(StringUtils.isEmpty(value3) || "null".equals(value3)) count++;
                            }
                            if(count==value2.size()) continue;
                            if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                            if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                            String concatSql = sql+") "+sql2+");";
                            finalSql+=concatSql;
                        }
                    }
                }
            }
        }
        return finalSql;
    }
    public String insertAjxxSql(Map<String, Object> map,Long folder_id) {
        String tenantCode = SecurityUtils.getTenantCode();
        ArrayList<Map<String, Object>> list =(ArrayList<Map<String, Object>>)map.get("AJXXLIST");
        String finalSql = "";
        if(null!=list){
            if(list.size()!=0) {
                for(int i=0;i<list.size();i++) {
                    Map<String, Object> map2 = list.get(i);
                    Set<Entry<String,Object>> entrySet = map2.entrySet();
                    Iterator<Entry<String, Object>> iterator = entrySet.iterator();
                    while(iterator.hasNext()) {
                        Entry<String, Object> next = iterator.next();
                        String key = next.getKey();   //审查起诉案件信息
                        if("审查起诉案件信息".equals(key)) {
                            Map<String, Object> value = (HashMap<String, Object>)next.getValue();
                            Set<Entry<String,Object>> entrySet2 = value.entrySet();
                            Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                            while(iterator2.hasNext()) {
                                Entry<String, Object> next2 = iterator2.next();
                                String key2 = next2.getKey();   //TQKSQK
                                Long id2 = SnowflakeIdGenerator.generateId();
                                String sql="insert into "+tenantCode+"_AKSC"+key2+"(id,folder_id,";
                                String sql2="values("+id2+","+folder_id+",";
                                Map<String, Object> value2 = (HashMap<String, Object>)next2.getValue();
                                Set<Entry<String,Object>> entrySet3 = value2.entrySet();
                                Iterator<Entry<String, Object>> iterator3 = entrySet3.iterator();
                                int count = 0;
                                while(iterator3.hasNext()) {
                                    Entry<String, Object> next3 = iterator3.next();
                                    String key3 = next3.getKey();
                                    String value3 = String.valueOf(next3.getValue());
                                    sql+=""+key3+",";
                                    sql2+="'"+value3+"',";
                                    if(StringUtils.isEmpty(value3) || "null".equals(value3)) count++;
                                }
                                if(count==value2.size()) continue;
                                if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                                if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                                String concatSql = sql+") "+sql2+");";
                                finalSql+=concatSql;
                            }
                        }else if("审判活动监督情况".equals(key)) {
                            Long id2 = SnowflakeIdGenerator.generateId();
                            String sql="insert into "+tenantCode+"_AKSP(id,folder_id,";
                            String sql2="values("+id2+","+folder_id+",";
                            Map<String, Object> value = (HashMap<String, Object>)next.getValue();
                            Set<Entry<String,Object>> entrySet2 = value.entrySet();
                            Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                            int count = 0;
                            while(iterator2.hasNext()) {
                                Entry<String, Object> next3 = iterator2.next();
                                String key3 = next3.getKey();
                                String value3 = String.valueOf(next3.getValue());
                                sql+=""+key3+",";
                                sql2+="'"+value3+"',";
                                if(StringUtils.isEmpty(value3) || "null".equals(value3)) count++;
                            }
                            if(count==value.size()) continue;
                            if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                            if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                            String concatSql = sql+") "+sql2+");";
                            finalSql+=concatSql;
                        }else if("线索移交情况".equals(key)) {
                            Long id2 = SnowflakeIdGenerator.generateId();
                            String sql="insert into "+tenantCode+"_AKXS(id,folder_id,";
                            String sql2="values("+id2+","+folder_id+",";

                            Map<String, Object> value = (HashMap<String, Object>)next.getValue();
                            Set<Entry<String,Object>> entrySet2 = value.entrySet();
                            Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
                            int count = 0;
                            while(iterator2.hasNext()) {
                                Entry<String, Object> next3 = iterator2.next();
                                String key3 = next3.getKey();
                                String value3 = String.valueOf(next3.getValue());
                                sql+=""+key3+",";
                                sql2+="'"+value3+"',";
                                if(StringUtils.isEmpty(value3) || "null".equals(value3)) count++;
                            }
                            if(count==value.size()) continue;
                            if(sql.endsWith(",")) sql = sql.substring(0, sql.length()-1);
                            if(sql2.endsWith(",")) sql2 = sql2.substring(0, sql2.length()-1);
                            String concatSql = sql+") "+sql2+");";
                            finalSql+=concatSql;
                        }
                        //String sql1 = "insert into AKFZXYR(id,name,folder_id) values("+id+",'"+key+"',"+folder_id+");";
                        //finalSql+=sql1;
                    }

                }
            }
        }
        return finalSql;
    }

    public String getDocSql(ArrayList<Map<String, Object>> list, Long da_id,
                            Map<String, Map<String, String>> dictByGroupId,
                            String zippedPath, String efiletablename, TenantCategory tenantCategory, Long template_id) throws IOException {
        String tenantCode = SecurityUtils.getTenantCode();
        //HashMap<String,String> fileSqlList = new HashMap<>();
        StringBuilder fcolumnsBuilder = new StringBuilder();
        StringBuilder fvalueBuilder = new StringBuilder();
        Map<String, Object> docmdataList = getMdataList(template_id,2);
        loger.info("docmdataList---relation：：：{}",docmdataList.toString());
        String yq_mnt = sysConfigService.selectConfigByKey("YQ_MNT");
        String insertFileSql = "";
        Integer orderNo = 0;
        for(int i=0;i<list.size();i++) {
            String wjlx = "1";
            boolean iswjdx = true;
            boolean iswjlx = false;
            Long doc_id = SnowflakeIdGenerator.generateId();
            Map<String, Object> map2 = list.get(i);
            Set<Entry<String,Object>> entrySet2 = map2.entrySet();
            Iterator<Entry<String, Object>> iterator2 = entrySet2.iterator();
            while(iterator2.hasNext()) {
                Entry<String, Object> next2 = iterator2.next();
                String key2 = next2.getKey();
                Object value2 = next2.getValue();

                if(docmdataList.containsKey(key2)) {
                    String concatVal = String.valueOf(docmdataList.get(key2));
                    String[] valArray = concatVal.split(":");
                    String field_name = valArray[0];  //数据库字段名
                    if("PATH".equalsIgnoreCase(field_name)|| "DOC_NAME".equalsIgnoreCase(field_name) ||
                            "ACTION_TIME".equalsIgnoreCase(field_name)|| "CREATE_TIME".equalsIgnoreCase(field_name)){
                        continue;
                    }
                    String fieldtype = valArray[1];   //字段
                    String showtype = valArray[2];	  //展示类型
                    if(i==0) {   //列名只保存一遍
                        fcolumnsBuilder.append(field_name).append(",");
                    }
                    if("5".equals(showtype.trim())) {  //如果有字典项
                        Map<String, String> dictMap = dictByGroupId.get(field_name);
                        String dictVal = dictMap.get(value2);
                        if(!StringUtils.isEmpty(dictVal)) {
                            value2 = dictVal;
                        }
                    }
                    if("GB".equalsIgnoreCase(field_name) && (StringUtils.isEmpty(value2.toString()) || "null".equals(value2))){
                        wjlx = "1";
                        iswjlx = true;
                    }else if("GB".equalsIgnoreCase(field_name)){
                        iswjlx = true;
                        wjlx = String.valueOf(value2);
                    }
                    if("TYPE".equalsIgnoreCase(field_name)){
                        wjlx = String.valueOf(value2);
                    }
                    if("WJDX".equalsIgnoreCase(field_name) && (StringUtils.isEmpty(value2.toString()) || "null".equals(value2))){
                        iswjdx = false;
                    }

                    if("1".equals(fieldtype.trim())) {   //整型
                        fvalueBuilder.append(value2).append(",");
                    }else if("2".equals(fieldtype.trim())) {   //字符型
                        fvalueBuilder.append("'"+value2+"',");
                    }else{
                        fvalueBuilder.append("'"+value2+"',");
                    }
                }
            }
            String filename = map2.get("WJMC")+"."+map2.get("GSXX");
            String format = filename.substring(filename.lastIndexOf(".")+1);

            File file = new File(zippedPath+"/Files/"+filename);
            if(file.exists()){

                TenantYqdaDocument document = new TenantYqdaDocument();
                document.setConTableId(da_id);
                document.setTableName(efiletablename);
                if(orderNo==0){
                    String orderNoStr = documentMapper.MaxOrderNo(document);
                    //获取序号
                    orderNo =  StringUtils.isEmpty(orderNoStr) ? 1:(Integer.valueOf(orderNoStr)+1);
                }else{
                    orderNo = orderNo + 1;
                }

                String savefilePath = FileUploadUtils.extractPath(tenantCategory,da_id);  //文件路径
                String savefileName = extractFilename(Integer.valueOf(wjlx),da_id,orderNo,format);  //保存文件名
                File newFile = new File(yq_mnt+savefilePath+savefileName);
                loger.info("生成文件路径：：：{}",yq_mnt+savefilePath+savefileName);
                FileUtils.copyFile(file,newFile);
                // 获取文件创建时间
                Path path2 = Paths.get(yq_mnt+savefilePath+savefileName);
                // 获取文件属性
                BasicFileAttributes attributes = Files.readAttributes(path2, BasicFileAttributes.class);
                // 获取文件创建时间
                FileTime createTime = attributes.creationTime();
                // 将文件创建时间转换为标准格式
                String dateFormat = df.format(createTime.toMillis());

                insertFileSql += "insert into "+efiletablename+"(doc_id,"+fcolumnsBuilder+"doc_name,con_table_id,doc_order,is_del,action_time,create_time,is_index,real_doc_name,doc_mnt," +
                        "create_by,path) " +
                        "values("+doc_id+","+fvalueBuilder.toString()+"'"+filename+"',"+da_id+","+orderNo+",0,'"+dateFormat+"','"+DateUtils.getTime()+"',0,'"+savefileName+"','"+yq_mnt+"'," +
                        "'"+map2.get("IDH")+"','"+savefilePath+savefileName+"');";
                insertFileSql+="update "+efiletablename+" set doc_file_type='"+format.toUpperCase()+"'"+(!iswjdx?",doc_file_size='"+newFile.length()+"'":"")+"" +
                        " "+(iswjlx?",type="+wjlx+"":"")+" where doc_id = "+doc_id+";";
                insertFileSql+="update "+tenantCode+"_yqda set efile_count = efile_count+1 where da_id = "+da_id+";";
                insertFileSql+="update "+tenantCategory.getFileTableName()+" set efile_count = efile_count+1 where id = "+da_id+";";
            }

            fvalueBuilder.setLength(0);
        }

        return insertFileSql;
    }
    public static final String extractFilename(Integer type , Long daId, Integer orderNo, String ext)
    {
        String fileName = daId+"0"+type+"A"+ (orderNo<999 ? (orderNo<99 ? (orderNo<9 ? "00"+orderNo:("0"+orderNo)):orderNo):orderNo);
        StringBuilder path = new StringBuilder().append(fileName).append(".").append(ext);
        return path.toString();
    }

    public List<Map<String, Object>> getNotEmptyColumn(Long group_id,int type){
        String tenantCode = SecurityUtils.getTenantCode();
        // group_id = 1 文书组件
        String sql = "select FIELD_NAME,FIELDTYPE,SHOWTYPE from "+tenantCode+"_mdata_list where GROUP_ID = '"+group_id+"'  and TYPE = '"+type+"'"
                +" and is_no_empty = 1 and field_name not in ('IS_DEL','STATE','IS_INDEX','DA_ID','CREATE_TIME','QZH','TYPE','JS_TYPE')";
        List<Map<String, Object>> listResult = yqdaarticulatedMapper.executeSql(sql);
        return listResult;
    }

    public Map<String, Object> getMdataList(Long group_id,int type){
        String tenantCode = SecurityUtils.getTenantCode();
        Map<String, Object> listResult = new HashMap<>();
        // 加映射字段  OA_FIELD_NAME 以及  group_id = 1 文书组件
        String sql = "select FIELD_NAME,FIELDTYPE,SHOWTYPE,RELE_FIELD_NAME from "+tenantCode+"_mdata_list where GROUP_ID = '"+group_id+"'  and TYPE = '"+type+"' ";
        List<Map<String,Object>> listResult2 = yqdaarticulatedMapper.executeSql(sql);
        for(Map<String,Object> map:listResult2) {
            String oa_field_name = String.valueOf(map.get("RELE_FIELD_NAME"));
            if(!StringUtils.isEmpty(oa_field_name) && !"null".equals(oa_field_name)) {
                listResult.put(oa_field_name, map.get("FIELD_NAME")+":"+map.get("FIELDTYPE")+":"+map.get("SHOWTYPE"));
            }
        }
        return listResult;
    }

    /**
     * 拼接xml路径
     * @param zippedPath
     * @return
     */
   /* public String getConcatPath(String zippedPath){

    }*/
    public Map<String,Object> analysisXmlOfDataHead(String zippedPath) throws Exception{
        HashMap<String,Object> map = new HashMap<>();

        SAXReader reader = new SAXReader();
        FileInputStream in = new FileInputStream(zippedPath);
        String yq_oacharset = sysConfigService.selectConfigByKey("YQ_OACHARSET");
        if(!StringUtils.isNotEmpty(yq_oacharset) || "null".equals(yq_oacharset))
            yq_oacharset = "UTF-8";
        Reader read = new InputStreamReader(in,yq_oacharset);
        Document document = reader.read(read);
        //获取所有的一级子节点
        Element rootElement = document.getRootElement();
        List child = rootElement.content();
        if (child!=null && child.size()!=0){
            for (int i = 0; i < child.size(); i++) {
                if (child.get(i) instanceof DefaultElement){
                    DefaultElement element = (DefaultElement) child.get(i);
                    QName qName = element.getQName();
                    if(qName!=null && "HEAD".equals(qName.getName())) {
                        List elementList = element.content();
                        //System.out.println("==========fields==========");
                        for (int j = 0; j < elementList.size(); j++) {
                            if (elementList.get(j) instanceof DefaultElement){
                                //所有子数据的节点
                                DefaultElement defaultElement = (DefaultElement) elementList.get(j);
                                //QName qName2 = defaultElement.getQName();
                                if(!StringUtils.isEmpty(defaultElement.getText()))
                                    map.put(defaultElement.getName(), defaultElement.getText());
                            }
                        }
                    }
                }
            }
        }

        return map;
    }

    public Map<String,Object> analysisXmlOfGlxx(String zippedPath) throws Exception{
        HashMap<String,Object> map = new HashMap<>();

        map.put("GLWJSTXX", new ArrayList<Map<String,Object>>());
        map.put("GLYWGLXX", new ArrayList<Map<String,Object>>());

        SAXReader reader = new SAXReader();
        FileInputStream in = new FileInputStream(zippedPath);
        String yq_oacharset = sysConfigService.selectConfigByKey("YQ_OACHARSET");
        if(!StringUtils.isNotEmpty(yq_oacharset) || "null".equals(yq_oacharset))
            yq_oacharset = "UTF-8";
        Reader read = new InputStreamReader(in,yq_oacharset);
        Document document = reader.read(read);
        //获取所有的一级子节点
        Element rootElement = document.getRootElement();
        List child = rootElement.content();
        if (child!=null && child.size()!=0){
            for (int i = 0; i < child.size(); i++) {
                if (child.get(i) instanceof DefaultElement){
                    DefaultElement element = (DefaultElement) child.get(i);
                    QName qName = element.getQName();
                    if(qName!=null && "GLNR".equals(qName.getName())) {
                        List elementList = element.content();
                        //System.out.println("==========fields==========");

                        for (int j = 0; j < elementList.size(); j++) {
                            if (elementList.get(j) instanceof DefaultElement){
                                //所有子数据的节点
                                DefaultElement defaultElement = (DefaultElement) elementList.get(j);
                                QName qName2 = defaultElement.getQName();
                                if(qName2!=null && "DZWJGDDJB".equals(qName2.getName())) {
                                    addGlnr(map,defaultElement,"DZWJGDDJB");
                                }else if(qName2!=null && "GDYJPZ".equals(qName2.getName())) {
                                    //如果有一层管理活动的话
                                    List elementList2 = defaultElement.content();
                                    for (int k = 0; k < elementList2.size(); k++) {
                                        if (elementList2.get(k) instanceof DefaultElement){
                                            DefaultElement defaultElement2 = (DefaultElement) elementList2.get(k);
                                            QName qName3 = defaultElement2.getQName();
                                            if(qName3!=null && "GLHD".equals(qName3.getName())) {
                                                defaultElement = defaultElement2;
                                            }
                                        }
                                    }
                                    addGlnr(map,defaultElement,"GDYJPZ");
                                }else if(qName2!=null && "XGSPB".equals(qName2.getName())) {
                                    addGlnr(map,defaultElement,"XGSPB");
                                }

                            }
                        }
                    }
                }
            }
        }
        return map;
    }
    public void addGlnr(Map<String,Object> map,DefaultElement defaultElement,String type){
        Map<String,Object> resMap = new HashMap<>();
        List elementList2 = defaultElement.content();
        for (int k = 0; k < elementList2.size(); k++) {
            if (elementList2.get(k) instanceof DefaultElement){
                DefaultElement defaultElement2 = (DefaultElement) elementList2.get(k);
                QName qName3 = defaultElement2.getQName();
                if(qName3!=null && "MLXX".equals(qName3.getName())) {
                    List elementList3 = defaultElement2.content();
                    for(int m=0;m<elementList3.size();m++) {
                        if (elementList3.get(m) instanceof DefaultElement){
                            DefaultElement defaultElement3 = (DefaultElement) elementList3.get(m);
                            String text = defaultElement3.getText();
                            if(!StringUtils.isEmpty(text))
                                resMap.put(defaultElement3.getName(), defaultElement3.getText());

                        }
                    }
                    map.put("GL"+type+"MLXX",resMap);
                }else if(qName3!=null && "WJSTXX".equals(qName3.getName())) {
                    List elementList3 = defaultElement2.content();
                    HashMap<String, Object> hashMap = new HashMap<>();
                    for(int m=0;m<elementList3.size();m++) {
                        if (elementList3.get(m) instanceof DefaultElement){
                            DefaultElement defaultElement3 = (DefaultElement) elementList3.get(m);
                            if(!StringUtils.isEmpty(defaultElement3.getText()))
                                hashMap.put(defaultElement3.getName(), defaultElement3.getText());
                        }
                    }
                    if(hashMap.size()!=0) hashMap.put("TYPE",type);
                    List<Map<String,Object>> l = (List<Map<String,Object>>)map.get("GLWJSTXX");
                    l.add(hashMap);
                }else if(qName3!=null && "YWGLXX".equals(qName3.getName())){
                    List elementList3 = defaultElement2.content();

                    for(int m=0;m<elementList3.size();m++) {
                        if (elementList3.get(m) instanceof DefaultElement){
                            DefaultElement defaultElement3 = (DefaultElement) elementList3.get(m);
                            QName qName = defaultElement3.getQName();
                            if(qName!=null && "GLHD".equals(qName.getName())){
                                HashMap<String, Object> hashMap = new HashMap<>();
                                List elementList4 = defaultElement3.content();
                                for(int n=0;n<elementList4.size();n++){
                                    if (elementList4.get(n) instanceof DefaultElement){
                                        DefaultElement defaultElement4 = (DefaultElement) elementList4.get(n);
                                        if(!StringUtils.isEmpty(defaultElement4.getText()))
                                            hashMap.put(defaultElement4.getName(), defaultElement4.getText());
                                    }
                                }
                                if(hashMap.size()!=0) hashMap.put("TYPE",type);
                                List<Map<String,Object>> l = (List<Map<String,Object>>)map.get("GLYWGLXX");
                                l.add(hashMap);
                            }
                        }
                    }

                }
            }
        }
    }

    public Map<String,Object> analysisXml(String zippedPath) throws Exception {
        HashMap<String,Object> map = new HashMap<>();


            SAXReader reader = new SAXReader();
            FileInputStream in = new FileInputStream(zippedPath);
            String yq_oacharset = sysConfigService.selectConfigByKey("YQ_OACHARSET");
            if(!StringUtils.isNotEmpty(yq_oacharset) || "null".equals(yq_oacharset))
                yq_oacharset = "UTF-8";
            Reader read = new InputStreamReader(in,yq_oacharset);
            Document document = reader.read(read);
            //获取所有的一级子节点
            Element rootElement = document.getRootElement();
            List child = rootElement.content();
            if (child!=null && child.size()!=0){
                for (int i = 0; i < child.size(); i++) {
                    if (child.get(i) instanceof DefaultElement){
                        DefaultElement element = (DefaultElement) child.get(i);
                        QName qName = element.getQName();
                        if(qName!=null && "AJNR".equals(qName.getName())) {
                            List elementList = element.content();
                            //System.out.println("==========fields==========");
                            for (int j = 0; j < elementList.size(); j++) {
                                if (elementList.get(j) instanceof DefaultElement){
                                    //所有子数据的节点
                                    DefaultElement defaultElement = (DefaultElement) elementList.get(j);
                                    QName qName2 = defaultElement.getQName();
                                    if(qName2!=null && "AJXX".equals(qName2.getName())) {
                                        List elementList2 = defaultElement.content();
                                        for (int k = 0; k < elementList2.size(); k++) {
                                            if (elementList2.get(k) instanceof DefaultElement){
                                                DefaultElement defaultElement2 = (DefaultElement) elementList2.get(k);
                                                QName qName3 = defaultElement2.getQName();
                                                if(qName3!=null && "JP".equals(qName3.getName())) {
                                                    List elementList3 = defaultElement2.content();
                                                    for(int m=0;m<elementList3.size();m++) {
                                                        if (elementList3.get(m) instanceof DefaultElement){
                                                            DefaultElement defaultElement3 = (DefaultElement) elementList3.get(m);
                                                            if(!StringUtils.isEmpty(defaultElement3.getText())) map.put(defaultElement3.getName(), defaultElement3.getText());

                                                        }
                                                    }
                                                }else if(qName3!=null && "ML".equals(qName3.getName())) {

                                                    if(!map.containsKey("children")) {
                                                        map.put("children", new ArrayList<Map<String, Object>>());
                                                    }
                                                    ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>)map.get("children");
                                                    HashMap<String,Object> map2 = new HashMap<String, Object>();

                                                    List elementList3 = defaultElement2.content();
                                                    for(int m=0;m<elementList3.size();m++) {
                                                        if (elementList3.get(m) instanceof DefaultElement){
                                                            DefaultElement defaultElement3 = (DefaultElement) elementList3.get(m);
                                                            QName qName4 = defaultElement3.getQName();


                                                            if(qName4!=null && "DZWJ".equals(qName4.getName())) {

                                                                List elementList4 = defaultElement3.content();
                                                                for(int n=0;n<elementList4.size();n++) {
                                                                    if (elementList4.get(n) instanceof DefaultElement){
                                                                        DefaultElement defaultElement4 = (DefaultElement) elementList4.get(n);
                                                                        QName qName5 = defaultElement4.getQName();
                                                                        if(qName5!=null && "WJSTXX".equals(qName5.getName())) {

                                                                            if(!map2.containsKey("children")) {
                                                                                map2.put("children", new ArrayList<Map<String, Object>>());
                                                                            }
                                                                            ArrayList<Map<String, Object>> list2 = (ArrayList<Map<String, Object>>)map2.get("children");
                                                                            HashMap<String,Object> map3 = new HashMap<String, Object>();

                                                                            List elementList5 = defaultElement4.content();
                                                                            for(int o=0;o<elementList5.size();o++) {
                                                                                if(elementList5.get(o) instanceof DefaultElement) {
                                                                                    DefaultElement defaultElement5 = (DefaultElement) elementList5.get(o);
                                                                                    if(!StringUtils.isEmpty(defaultElement5.getText())) map3.put(defaultElement5.getName(), defaultElement5.getText());
                                                                                }
                                                                                //DefaultElement defaultElement5 = (DefaultElement)elementList5.get(o);

                                                                            }
                                                                            list2.add(map3);

                                                                        }
                                                                    }
                                                                }
                                                            }else {
                                                                if(!StringUtils.isEmpty(defaultElement3.getText())) map2.put(defaultElement3.getName(), defaultElement3.getText());
                                                            }
                                                        }
                                                    }
                                                    list.add(map2);
                                                }else if(qName3!=null && "BKB".equals(qName3.getName())){
                                                    List elementList3 = defaultElement2.content();
                                                    HashMap<String, Object> resMap = new HashMap<>();
                                                    for(int m=0;m<elementList3.size();m++) {
                                                        if (elementList3.get(m) instanceof DefaultElement){
                                                            DefaultElement defaultElement3 = (DefaultElement) elementList3.get(m);
                                                            if(!StringUtils.isEmpty(defaultElement3.getText()))
                                                                resMap.put(defaultElement3.getName(), defaultElement3.getText());

                                                        }
                                                    }
                                                    map.put("BKB",resMap);
                                                }
                                            }
                                        }
                                    }else if(qName2!=null && "DSRXX".equals(qName2.getName())) {
                                        if(!map.containsKey("DSRXXLIST")) {
                                            map.put("DSRXXLIST", new ArrayList<Map<String, Object>>());
                                        }
                                        ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>)map.get("DSRXXLIST");
                                        HashMap<String,Object> map2 = new HashMap<String, Object>();

                                        List elementList2 = defaultElement.content();
                                        for (int k = 0; k < elementList2.size(); k++) {
                                            if (elementList2.get(k) instanceof DefaultElement){
                                                DefaultElement defaultElement2 = (DefaultElement) elementList2.get(k);
                                                QName qName3 = defaultElement2.getQName();
                                                if(qName3!=null && "DSR".equals(qName3.getName())) {
                                                    List elementList3 = defaultElement2.content();
                                                    for(int m=0;m<elementList3.size();m++) {
                                                        if (elementList3.get(m) instanceof DefaultElement){
                                                            DefaultElement defaultElement3 = (DefaultElement) elementList3.get(m);

                                                            if(!StringUtils.isEmpty(defaultElement3.getText())) map2.put(defaultElement3.getName(), defaultElement3.getText());

                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        list.add(map2);
                                    }else if(qName2!=null && "AKXX".equals(qName2.getName())) {
                                        List elementList2 = defaultElement.content();
                                        for (int k = 0; k < elementList2.size(); k++) {
                                            if (elementList2.get(k) instanceof DefaultElement){
                                                DefaultElement defaultElement2 = (DefaultElement) elementList2.get(k);
                                                String attributeValue2 = defaultElement2.attributeValue("title");
                                                if("被害人/单位".equals(attributeValue2)) {
                                                    if(!map.containsKey("BHRLIST")) {
                                                        map.put("BHRLIST", new ArrayList<Map<String, Object>>());
                                                    }
                                                    ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>)map.get("BHRLIST");

                                                    List content = defaultElement2.content();
                                                    for (int m = 0; m < content.size(); m++) {
                                                        if (content.get(m) instanceof DefaultElement){
                                                            HashMap<String,Object> map2 = new HashMap<String, Object>();
                                                            DefaultElement defaultElement3 = (DefaultElement) content.get(m);
                                                            String attributeValue3 = defaultElement3.attributeValue("title");
                                                            //if("赵良".equals(attributeValue3)) {
                                                            List content2 = defaultElement3.content();
                                                            HashMap<String,Object> map3 = new HashMap<String, Object>();
                                                            for (int n = 0; n < content2.size(); n++) {
                                                                if (content2.get(n) instanceof DefaultElement){
                                                                    DefaultElement defaultElement4 = (DefaultElement) content2.get(n);

                                                                    //if(!StringUtils.isEmpty(defaultElement4.getText())) map2.put(defaultElement4.getName(), defaultElement4.getText());

                                                                    String attributeValue4 = defaultElement4.attributeValue("title");
                                                                    //if("二审不开庭裁判情况".equals(attributeValue4)) {
                                                                    List content3 = defaultElement4.content();
                                                                    //String sql = "create table \"JCYDACP\".\"AKFZESBKTCPQK\"(";
                                                                    //sql+="\"ID\" INT NOT NULL,\"FOLDER_ID\" INT,";
                                                                    for (int p = 0; p < content3.size(); p++) {
                                                                        if (content3.get(p) instanceof DefaultElement){
                                                                            DefaultElement defaultElement5 = (DefaultElement) content3.get(p);
                                                                            String name = defaultElement5.getName();
                                                                            String text = defaultElement5.getText();
                                                                            //		sql+="\""+name+"\" VARCHAR(50),";
                                                                            if(!StringUtils.isEmpty(text)) map3.put(name, text);

                                                                        }
                                                                    }
                                                                    //sql+="CONSTRAINT \"TBL_FZESBKTCPQK_PKEY\" PRIMARY KEY (\"ID\")) "
                                                                    //		+ " TABLESPACE \"SYSTEM\" INIT 64K NEXT 64K MAXSIZE UNLIMITED  PCTFREE 10 PCTUSED 40";
                                                                    //DbTool.executeSql(sql);
                                                                    //}
                                                                }
                                                            }
                                                            map2.put(attributeValue3, map3);
                                                            //}
                                                            list.add(map2);
                                                        }
                                                    }
                                                }else if("案件信息".equals(attributeValue2)) {
                                                    if(!map.containsKey("AJXXLIST")) {
                                                        map.put("AJXXLIST", new ArrayList<Map<String, Object>>());
                                                    }
                                                    ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>)map.get("AJXXLIST");

                                                    List content = defaultElement2.content();
                                                    for (int m = 0; m < content.size(); m++) {  //循环人员 张三
                                                        if (content.get(m) instanceof DefaultElement){
                                                            HashMap<String,Object> map2 = new HashMap<String, Object>();
                                                            DefaultElement defaultElement3 = (DefaultElement) content.get(m);
                                                            String attributeValue3 = defaultElement3.attributeValue("title");
                                                            if("审查起诉案件信息".equals(attributeValue3)) {

                                                                //if("赵良".equals(attributeValue3)) {
                                                                List content2 = defaultElement3.content();
                                                                //map2.put(attributeValue3, value)
                                                                HashMap<String,Object> map3 = new HashMap<String, Object>();
                                                                for (int n = 0; n < content2.size(); n++) {


                                                                    if (content2.get(n) instanceof DefaultElement){
                                                                        DefaultElement defaultElement4 = (DefaultElement) content2.get(n);

                                                                        if(!StringUtils.isEmpty(defaultElement4.getText())) map2.put(defaultElement4.getName(), defaultElement4.getText());

                                                                        String attributeValue4 = defaultElement4.attributeValue("title");

                                                                        //if("二审不开庭裁判情况".equals(attributeValue4)) {
                                                                        List content3 = defaultElement4.content();
                                                                        //String sql = "create table \"JCYDACP\".\"AKFZESBKTCPQK\"(";
                                                                        //sql+="\"ID\" INT NOT NULL,\"FOLDER_ID\" INT,";
                                                                        HashMap<String,Object> map4 = new HashMap<String, Object>();
                                                                        for (int p = 0; p < content3.size(); p++) {
                                                                            if (content3.get(p) instanceof DefaultElement){
                                                                                DefaultElement defaultElement5 = (DefaultElement) content3.get(p);
                                                                                String name = defaultElement5.getName();
                                                                                String text = defaultElement5.getText();
                                                                                if(!StringUtils.isEmpty(text)) map4.put(name, text);

                                                                                //		sql+="\""+name+"\" VARCHAR(50),";

                                                                            }
                                                                        }
                                                                        if("基本信息".equals(attributeValue4)) {
                                                                            map3.put("JBXX", map4);
                                                                        }
                                                                        if("侵犯知识产权情况".equals(attributeValue4)) {
                                                                            map3.put("QFZSCQQK", map4);
                                                                        }
                                                                        if("办理情况".equals(attributeValue4)) {
                                                                            map3.put("BLQK", map4);
                                                                        }
                                                                        if("审结情况".equals(attributeValue4)) {
                                                                            map3.put("SJQK", map4);
                                                                        }
                                                                        if("案件其他有关情况".equals(attributeValue4)) {
                                                                            map3.put("AJQTYGQK", map4);
                                                                        }
                                                                        if("毒品犯罪有关情况".equals(attributeValue4)) {
                                                                            map3.put("DPFZYGQK", map4);
                                                                        }
                                                                        if("出庭裁判情况".equals(attributeValue4)) {
                                                                            map3.put("CTCPQK", map4);
                                                                        }
                                                                        if("提出抗诉情况".equals(attributeValue4)) {
                                                                            map3.put("TCKSQK", map4);
                                                                        }
                                                                        if("提请抗诉情况".equals(attributeValue4)) {
                                                                            map3.put("TQKSQK", map4);
                                                                        }
                                                                        //sql+="CONSTRAINT \"TBL_FZESBKTCPQK_PKEY\" PRIMARY KEY (\"ID\")) "
                                                                        //		+ " TABLESPACE \"SYSTEM\" INIT 64K NEXT 64K MAXSIZE UNLIMITED  PCTFREE 10 PCTUSED 40";
                                                                        //DbTool.executeSql(sql);
                                                                        //}
                                                                    }
                                                                }

                                                                //}

                                                                map2.put(attributeValue3, map3);

                                                            }else {
                                                                List content2 = defaultElement3.content();
                                                                HashMap<String,Object> map3 = new HashMap<String, Object>();
                                                                for (int n = 0; n < content2.size(); n++) {
                                                                    if (content2.get(n) instanceof DefaultElement){
                                                                        DefaultElement defaultElement4 = (DefaultElement) content2.get(n);

                                                                        //if(!StringUtils.isEmpty(defaultElement4.getText())) map3.put(defaultElement4.getName(), defaultElement4.getText());

                                                                        String attributeValue4 = defaultElement4.attributeValue("title");
                                                                        //if("二审不开庭裁判情况".equals(attributeValue4)) {
                                                                        List content3 = defaultElement4.content();
                                                                        //String sql = "create table \"JCYDACP\".\"AKFZESBKTCPQK\"(";
                                                                        //sql+="\"ID\" INT NOT NULL,\"FOLDER_ID\" INT,";
                                                                        for (int p = 0; p < content3.size(); p++) {
                                                                            if (content3.get(p) instanceof DefaultElement){
                                                                                DefaultElement defaultElement5 = (DefaultElement) content3.get(p);
                                                                                String name = defaultElement5.getName();
                                                                                String text = defaultElement5.getText();
                                                                                if(!StringUtils.isEmpty(text)) map3.put(name, text);

                                                                                //		sql+="\""+name+"\" VARCHAR(50),";

                                                                            }
                                                                        }
                                                                        //sql+="CONSTRAINT \"TBL_FZESBKTCPQK_PKEY\" PRIMARY KEY (\"ID\")) "
                                                                        //		+ " TABLESPACE \"SYSTEM\" INIT 64K NEXT 64K MAXSIZE UNLIMITED  PCTFREE 10 PCTUSED 40";
                                                                        //DbTool.executeSql(sql);
                                                                        //}
                                                                    }
                                                                }
                                                                map2.put(attributeValue3, map3);

                                                            }
                                                            list.add(map2);
                                                        }
                                                    }
                                                }else if("犯罪嫌疑人/单位".equals(attributeValue2)) {
                                                    if(!map.containsKey("FZXYRLIST")) {
                                                        map.put("FZXYRLIST", new ArrayList<Map<String, Object>>());
                                                    }
                                                    ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>)map.get("FZXYRLIST");

                                                    List content = defaultElement2.content();
                                                    for (int m = 0; m < content.size(); m++) {  //循环人员 张三
                                                        if (content.get(m) instanceof DefaultElement){
                                                            HashMap<String,Object> map2 = new HashMap<String, Object>();
                                                            DefaultElement defaultElement3 = (DefaultElement) content.get(m);
                                                            String attributeValue3 = defaultElement3.attributeValue("title");
                                                            //if("赵良".equals(attributeValue3)) {
                                                            List content2 = defaultElement3.content();
                                                            //map2.put(attributeValue3, value)
                                                            HashMap<String,Object> map3 = new HashMap<String, Object>();
                                                            HashMap<String,Object> map5 = new HashMap<String, Object>();
                                                            boolean isHasJbxx = true;
                                                            for (int n = 0; n < content2.size(); n++) {

                                                                if (content2.get(n) instanceof DefaultElement){
                                                                    DefaultElement defaultElement4 = (DefaultElement) content2.get(n);

                                                                    String attributeValue4 = defaultElement4.attributeValue("title");
                                                                    if(!"基本信息".equals(attributeValue4) && !"涉案情况".equals(attributeValue4) && !"审结情况".equals(attributeValue4)
                                                                            && !"审结变更情况".equals(attributeValue4)&& !"羁押必要性审查".equals(attributeValue4)&& !"一审裁判情况".equals(attributeValue4)
                                                                            && !"二审不开庭裁判情况".equals(attributeValue4)){    //没有犯罪嫌疑人的基本信息
                                                                        isHasJbxx = false;
                                                                        String name = defaultElement4.getName();
                                                                        String text = defaultElement4.getText();
                                                                        if(!StringUtils.isEmpty(text)) map5.put(name, text);
                                                                    }else{

                                                                        if(!StringUtils.isEmpty(defaultElement4.getText())) map2.put(defaultElement4.getName(), defaultElement4.getText());


                                                                        List content3 = defaultElement4.content();

                                                                        HashMap<String,Object> map4 = new HashMap<String, Object>();
                                                                        for (int p = 0; p < content3.size(); p++) {
                                                                            if (content3.get(p) instanceof DefaultElement){
                                                                                DefaultElement defaultElement5 = (DefaultElement) content3.get(p);
                                                                                String name = defaultElement5.getName();
                                                                                String text = defaultElement5.getText();
                                                                                if(!StringUtils.isEmpty(text)) map4.put(name, text);

                                                                            }
                                                                        }
                                                                        if("基本信息".equals(attributeValue4)) {
                                                                            map3.put("JBXX", map4);
                                                                        }
                                                                        if("涉案情况".equals(attributeValue4)) {
                                                                            map3.put("SAQK", map4);
                                                                        }
                                                                        if("审结情况".equals(attributeValue4)) {
                                                                            map3.put("SJQK", map4);
                                                                        }
                                                                        if("审结变更情况".equals(attributeValue4)) {
                                                                            map3.put("SJBGQK", map4);
                                                                        }
                                                                        if("羁押必要性审查".equals(attributeValue4)) {
                                                                            map3.put("JYBYXSC", map4);
                                                                        }
                                                                        if("一审裁判情况".equals(attributeValue4)) {
                                                                            map3.put("YSCPQK", map4);
                                                                        }
                                                                        if("二审不开庭裁判情况".equals(attributeValue4)) {
                                                                            map3.put("ESBKTCPQK", map4);
                                                                        }
                                                                    }

                                                                }
                                                            }
                                                            if(isHasJbxx){   //有犯罪嫌疑人的基本信息
                                                                map2.put(attributeValue3, map3);
                                                                list.add(map2);
                                                            }else{  //没有犯罪嫌疑人的基本信息
                                                                map3.put("JBXX", map5);
                                                                map2.put(attributeValue3, map3);
                                                                list.add(map2);
                                                            }
                                                        }
                                                    }
                                                }


                                            }
                                        }
                                    }


                                    //if(!StringUtils.isEmpty(defaultElement.getText())) map.put(defaultElement.getName(), defaultElement.getText());
                                    //System.out.println(defaultElement.getName()+"=="+defaultElement.getText());
                                }
                            }
                        }/*else if (qName!=null && "files".equals(qName.getName())){
                            List elementList = element.content();
                            List<Map<String,Object>> list1 = new ArrayList<Map<String, Object>>();
                            for (int j = 0; j < elementList.size(); j++) {
                                if (elementList.get(j) instanceof DefaultElement){
                                    List elementList1 = ((DefaultElement)elementList.get(j)).content();
                                    HashMap<String, Object> dirMap = new HashMap<String, Object>();
                                    //System.out.println("==========files==========");
                                    for (int k = 0; k < elementList1.size(); k++) {
                                        if (elementList1.get(k) instanceof DefaultElement){
                                            //所有子数据的节点
                                            DefaultElement defaultElement = (DefaultElement) elementList1.get(k);
                                            if(!StringUtils.isEmpty(defaultElement.getText())) dirMap.put(defaultElement.getName(), defaultElement.getText());
                                            //System.out.println(defaultElement.getName()+"=="+defaultElement.getText());
                                        }
                                    }
                                    list1.add(dirMap);
                                }
                            }
                            map.put("children", list1);
                        }*/
                    }
                }
            }
            //File tempFile = new File(zippedPath);
            //boolean removeTempFile = removeTempFile(tempFile);
            //if(removeTempFile) loger.info("删除临时文件成功！");
            //else loger.info("删除临时文件失败！");

        return map;
    }

    public boolean removeTempFile(File file){
        File[] files = file.listFiles();//将file子目录及子文件放进文件数组
        if (files!=null){//如果包含文件进行删除操作
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()){//删除子文件
                    files[i].delete();
                }else if(files[i].isDirectory()){//通过递归方法删除子目录的文件
                    removeTempFile(files[i]);
                }
                files[i].delete();//删除子目录
            }
        }
        return file.delete();
    }

    public boolean isNumeric(final CharSequence cs) {
        // 判断是否为空，如果为空则返回false
        if (StringUtils.isEmpty(cs)) {
            return false;
        }
        // 通过 length() 方法计算cs传入进来的字符串的长度，并将字符串长度存放到sz中
        final int sz = cs.length();
        // 通过字符串长度循环
        for (int i = 0; i < sz; i++) {
            // 判断每一个字符是否为数字，如果其中有一个字符不满足，则返回false
            if (!Character.isDigit(cs.charAt(i))) {
                return false;
            }
        }
        // 验证全部通过则返回true
        return true;
    }

    public void copy(File path1,File path2){
        try {
            BufferedInputStream bfis = new BufferedInputStream(new FileInputStream(path1));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));
            int len ;
            byte[] bytes = new byte[1024];
            while ((len=bfis.read(bytes))!=-1) {
                bos.write(bytes,0,len);
            }
            bfis.close();
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Map<String,Map<String, String>> getDictByGroupId(long groupId){
        String tenantCode = SecurityUtils.getTenantCode();
        String sql = "select M.FIELD_NAME,D.dict_value,D.dict_label from "+tenantCode+"_mdata_list M left join "+tenantCode+"_DICT_TYPE T on M.FIELD_NAME = T.dict_type left join "+tenantCode+"_DICT_data D on T.dict_type = D.dict_type where M.GROUP_ID = "+groupId+" and M.SHOWTYPE = 5";
        List<Map<String, Object>> dict = yqdaarticulatedMapper.executeSql(sql);
        Map<String,Map<String, String>> dictMap = new HashMap<>();
        Map<String, String> tempMap = null;
        for (int i = 0; i < dict.size(); i++) {
            Map<String, Object> map = dict.get(i);
            String fieldName = String.valueOf(map.get("FIELD_NAME"));
            String dictValue = String.valueOf(map.get("DICT_VALUE"));
            String dictLabel = String.valueOf(map.get("DICT_LABEL"));
            if (dictMap.get(fieldName)!=null) {//当前字典已存在
                tempMap = dictMap.get(fieldName);
            }else{//
                tempMap = new HashMap<>();
            }
            tempMap.put(dictLabel, dictValue);
            dictMap.put(fieldName,tempMap);
        }
        System.out.println("dict = "+dict);
        System.out.println("dictMap = "+dictMap);
        return dictMap;
    }


}
